﻿using System;
using System.Collections.Generic;
using System.Text;
using MindFactorial.LaunchPoint.Common;
using System.Reflection;
using System.Xml;

namespace MindFactorial.LaunchPoint.Runtime
{
    /// <summary>
    /// Represents a management object for getting access to specific runtime providers
    /// </summary>
    internal sealed class RuntimeConnectionProviderManager
    {
        static ConnectionProviderCollection _connectionProviders = new ConnectionProviderCollection();

        internal static ConnectionProviderCollection ConnectionProviders
        {
            get
            {
                return _connectionProviders;
            }
        }

        internal static void Initialize(Xml configuration)
        {
            Logger.Log("RuntimeConnectionProvider: Initializing connection provider with configuration {0}", configuration.Value);
            try
            {
                int providercount = 0;
                foreach (XmlNode providerNode in configuration.Node.ChildNodes)
                {
                    if (providerNode.Name == "ChannelProvider")
                    {
                        if (providerNode.Attributes["type"] != null)
                        {
                            try
                            {
                                string typevalue = providerNode.Attributes["type"].Value;
                                string name = providerNode.Attributes["Name"].Value;
                                string[] typeinfo = typevalue.Split(',');
                                string assembly = null;
                                string type = null;
                                if (typeinfo.Length == 2)
                                {
                                    assembly = typeinfo[1].Trim();
                                    type = typeinfo[0].Trim();
                                    Logger.Log("RuntimeConnectionProvider: Attempting to load connection provider {0} in assembly {1}", type, assembly);
                                    Assembly connectionProviderLib = Assembly.Load(assembly);
                                    RuntimeConnectionProvider connectionProvider = (RuntimeConnectionProvider)connectionProviderLib.CreateInstance(type, false);
                                    connectionProvider.LoadConfigInfo(providerNode, name);
                                    connectionProvider.Initialize();
                                    _connectionProviders.Add(connectionProvider);
                                    providercount++;
                                    Logger.Log("545F14DB-4889-4da2-94B6-87AF0125DCE6: RuntimeConnectionProvider: connection provider load succeeded for {0}{1}", type, assembly);
                                    
                                }
                                else
                                    Logger.Log("868DE486-3FD3-4890-BD8D-A70D3FB1791D: Communication Provider Type must be in a seperate assembly and specified in app config file with format format type='typename,assemblyname'");
                            }
                            catch (Exception ex)
                            {

                                Logger.Log("25BC27E-097F-482a-97EE-72CC41D72888: Could not load connection provider with configuration: {0} because {1}", configuration.Value,ex.Message);
                                
                            }


                        }
                        else
                        {
                            throw new Exception("868DE486-3FD3-4890-BD8D-A70D3FB1791D: No Runtime Communication Provider Found.");
                        }
                    }
                }
                if (providercount == 0)
                    throw new Exception("7581EA66-670F-4e4e-B2FA-178E22E97D05:No Runtime Communication Provider was loaded");
            }
            catch (Exception ex)
            {
                throw new LaunchPointRuntimeException("73CDD883-ED39-48a1-822C-2D0345D4258B:{0}", ex.Message);
            }
        }
    }
}
